package com.tenbit.tbw.svc;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tenbit.tbw.dao.EmployeeCodebookEDAO;
import com.tenbit.tbw.vo.EMP1010VO;
import com.tenbit.tbw.vo.EMP1020VO;
import com.tenbit.tbw.vo.EMP1030VO;
import com.tenbit.tbw.vo.EMP1040VO;

@Service
public class EMP1010SVC 
{
	
	@Autowired
	private EmployeeCodebookEDAO dao;
	
	public List<EMP1010VO> getEmployeeCodebook()
	{
		return dao.getEmployeeCodebookList();
	}
	
	public List<EMP1020VO> getEmployee(int page, int limit)
	{
		return dao.getEmployeeList(page, limit);
	}

	public int getTotalNum()
	{
		return dao.getTotal();
	}

	public Object getSearchTotalNum(Map<String, Object> map )
	{	
		boolean result=false;
		result=testSearchData(map);
		System.out.println(result);
		if(result){
			System.out.println(dao.getSearchTotal(map));
			return dao.getSearchTotal(map);
		}
		return null;
	}

	private boolean testSearchData(Map<String, Object> map) {
		
		boolean result=false;
		Calendar c = Calendar.getInstance();
		
		String graduationDate1=(String) map.get("graduationDate1");
		String graduationDate2=(String) map.get("graduationDate2");
		
		String[] gDate1=graduationDate1.split("-");
		String[] gDate2=graduationDate2.split("-");
		String year1=gDate1[0].trim();
		String year2=gDate2[0].trim();
		
		
		String month1=((String) map.get("month1")).trim();
		String month2=((String) map.get("month2")).trim();
		String day1=((String) map.get("day1")).trim();
		String day2=((String) map.get("day2")).trim();
		
		int sYear1=0;
		int sYear2=0;
		int sMonth1=Integer.parseInt(gDate1[1].trim());
		int sMonth2=Integer.parseInt(gDate2[1].trim());
		int sDay1=Integer.parseInt(gDate1[2].trim());
		int sDay2=Integer.parseInt(gDate2[2].trim());
		
		if(!year1.equals("yearDefault1")){
			sYear1=Integer.parseInt(year1);
		}
		if(!year2.equals("yearDefault2")){
			sYear2=Integer.parseInt(year2);
		}
		if(year1.equals("yearDefault1")&&(!month1.equals("monthDefault1")||!day1.equals("dayDefault1"))){
			return result;
		}
		if(year2.equals("yearDefault2")&&(!month2.equals("monthDefault2")||!day2.equals("dayDefault2"))){
			return result;
		}
		if(sYear1>0 && year2.equals("yearDefault2")){
			sYear2=2014;
		}
		if(sYear2>0 && year1.equals("yearDefault1")){
			sYear1=1900;
		} 
		if(month1.equals("monthDefault1")&& !day1.equals("dayDefault1")){
			return result;
		}
		if(month2.equals("monthDefault2") && !day2.equals("dayDefault2")){
			return result;
		}
		
		c.set(sYear1, sMonth1-1, 1);
		int lastDay1 = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		c.set(sYear2, sMonth2-1, 1);
		int lastDay2 = c.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		if(!year1.equals("yearDefault1")&&!month1.equals("monthDefault1")&&!day1.equals("dayDefault1")){
			if(sDay1>lastDay1){
				System.out.println(sDay1);
				System.out.println(lastDay1);
				System.out.println(5);
				return result;
			}
		}
		if(!year2.equals("yearDefault2")&&!month2.equals("monthDefault2")&&!day2.equals("dayDefault2")){
			if(sDay2>lastDay2){
				System.out.println(6);
				return result;
			}
		}
			if(sYear1>sYear2||(sYear1==sYear2 && sMonth1>sMonth2) || (sYear1==sYear2 && sMonth1==sMonth2 && sDay1>sDay2))
			{
				System.out.println(7);
				return result;
			}	
			
		result=true;
		
		return result;
	}

	public List<EMP1020VO> getSearchList(int page, int limit,Map<String, Object> map) 
	{
		boolean result=false;
		result=testSearchData(map);
		if(result){
			return dao.getSearch(page,limit,map);
		}
		return null;
	}

	public List<EMP1030VO> getEmployeeInfo(String empCode) {
		
		if(empCode!=null&& empCode!="" ){
			int empNum=Integer.parseInt(empCode);
			return dao.getInfo(empNum);
		}
		return null;
	}

	public List<EMP1040VO> getSkills(String empCode) {
		
		if(empCode!=null&& empCode!="" ){
			int empNum=Integer.parseInt(empCode);
			return dao.getSkillList(empNum);
		}
		return null;
	}

	public boolean setEmployeeInfo(Map<String, Object> map) {
		boolean result = false;
		
		result=checkInfo(map);
		if(result!=true){
			return result;
		}
			result=	dao.setEmpInfo(map);
		return result;
	}

	private boolean checkInfo(Map<String, Object> map) {
		boolean result=false;
		
		Calendar c = Calendar.getInstance();
		
		if(	map.get("employeeName")==null||map.get("employeeName").equals("")||
			map.get("idNumber")==null||map.get("idNumber").equals("")||
			map.get("genderCode")==null||map.get("genderCode").equals("")||
			map.get("departmentCode")==null||map.get("departmentCode").equals("")||
			map.get("graduationTypeCode")==null||map.get("graduationTypeCode").equals("")||
			map.get("graduationDate")==null||map.get("graduationDate").equals("")||
			map.get("baseSalary")==null||map.get("baseSalary").equals("")){
			
			System.out.println(1);
			
			return result;
		}
		String employeeName=(String) map.get("employeeName");
		if(employeeName.length()<2){
			
			System.out.println(2);
			
			return result;
		}
		
		String idNumber=(String) map.get("idNumber");
		String[] idNum=idNumber.split("-");
		
		String idNum1=idNum[0];
		String idNum2=idNum[1];
		
		if(idNumber.length()<14){
			
			System.out.println(3);
			
			return result;
	 	}
		
		int idNum1Year=Integer.parseInt(idNum1.substring(0,2));
		int idNum1Month=Integer.parseInt(idNum1.substring(2,4));
		int idNum1Day=Integer.parseInt(idNum1.substring(4,6));
		int idNum2Gender=Integer.parseInt(idNum2.substring(0,1));
		int idNumYear=0;
		int lastDayIdNum1=0;
		
		
		
		if(idNum2Gender<3){
			idNumYear=Integer.parseInt("19"+idNum1.substring(0,2)); 
			c.set(idNumYear, idNum1Month-1, 1);
			lastDayIdNum1=c.getActualMaximum(Calendar.DAY_OF_MONTH);
		} else if(idNum2Gender>2||idNum2Gender<5){
			idNumYear=Integer.parseInt("20"+idNum1.substring(0,2)); 
			c.set(idNumYear, idNum1Month-1, 1);
			lastDayIdNum1=c.getActualMaximum(Calendar.DAY_OF_MONTH);
		}  
		
		
		if(idNum1.length()!=6||idNum2.length()!=7){
			
			System.out.println(4);
			
			return result;
		}
		
		String departmentCode=(String) map.get("departmentCode");
		if(departmentCode.equals("")||departmentCode.equals("deptDefault")){
			
			System.out.println(5);
			
			return result;
		}
		
		String baseSalary=(String) map.get("baseSalary");
		if(baseSalary.length()<5){
			
			System.out.println(6);
			
			return result;
		}
		
		if(idNumYear<0||idNum1Month<1||idNum1Month>12||idNum1Day<1){
			
			System.out.println(7);
			
			return result;
		}
	 	if(idNum1Day>lastDayIdNum1){
	 		
	 		System.out.println(8);
	 		
			return result;
	 	}
	 	
		if(idNum2Gender<1||idNum2Gender>5){
			
			System.out.println(9);
			
			return result;
		} 
		
		String[] skillCode = (String[]) map.get("skillCode");
	 	String graduationTypeCode=(String) map.get("graduationTypeCode");
	 	boolean fl1=false;
	 	boolean fl2=false;
	 	for(int i=0;i<skillCode.length;i++){
	 		System.out.println(skillCode[i]);
	 	   if(skillCode[i].equals("S1")||skillCode[i].equals("S2")||skillCode[i].equals("S3")||skillCode[i].equals("S4")){
	 	    fl1=true;
	 	   }
	 	   System.out.println(fl1);
	 	}
	 	
	 	System.out.println(graduationTypeCode);
	 	if(graduationTypeCode.equals("G1")||graduationTypeCode.equals("G2")||graduationTypeCode.equals("G3")){
	 		fl2=true;
	 	}
	 	System.out.println(fl2);
	 	
	 	if(fl1!=true ||fl2!=true){
	 		
	 		System.out.println(10);
	 		
	 		return result;
	 	}
	 	if(!map.get("year1").equals("") && idNumYear>Integer.parseInt(((String) map.get("year1")).trim())){
	 		
	 		System.out.println(11);
	 		
			return result;
	 	}
	 	
	 	String graduationDate=(String) map.get("graduationDate");
	 	
	 	String[] gDate=graduationDate.split("-");
	 	
	 	int gYear=0;
	 	int gMonth=0;
	 	int gDay=0;
	 	int lastDayGdate=0;
	 	if(!gDate[0].equals("")){
	 		gYear=Integer.parseInt(gDate[0]);
	 	}
		if(!gDate[1].equals("")){
			gMonth=Integer.parseInt(gDate[1]);
	 	}
		if(!gDate[2].equals("")){
			gDay=Integer.parseInt(gDate[2]);
	 	}
	 	
		c.set(gYear, gMonth-1, 1);
		lastDayGdate=c.getActualMaximum(Calendar.DAY_OF_MONTH);

	 	
	 	if(gDay>lastDayGdate||gDay<0||lastDayGdate==0){
	 		
	 		System.out.println(12);
	 		
			return result;
		} 
	 	
	 	result=true;
	 	
		return result;
	}

	public boolean deleteEmployeeInfo(int empCode) {
		boolean result = false;
		if(empCode==0){
			return false;
		}	
		result = dao.deleteEmpInfo(empCode);
		return result;
	}

	public boolean insertEmployeeInfo(Map<String, Object> map) {
		boolean result = false;
		
		result=checkInfo(map);
		if(result!=true){
			return result;
		}
		result = dao.insertEmpInfo(map);
		System.out.println(result);
		return result;
	}

	public Integer getSearchEmpNum(String name) {
		if(name==null){
			name="";
		}
		return dao.getEmpNum(name);
	}

	public List<EMP1020VO> getSearchList(int page, int limit, String name) {
		if(name==null){
			name="";
		}
		return dao.getEmpList(page,limit,name);
	}

}
